<template>
  <div class="comment-post">
    <van-field class="post-field" v-model.trim="value" rows="2" autosize type="textarea" maxlength="50"
      placeholder="请输入留言" show-word-limit />
    <van-button class="post-btn" :disabled="!value" @click="onPostComment">发布</van-button>
  </div>
</template>

<script>
import { postComment } from '@/api/comment/index.js'
// 该组件会在不同的地方使用
// 对不同的目标进行发布评论
export default {
  name: 'CommentPost',
  components: {},
  props: {
    // 评论的目标id (文章id或评论项id)
    target: {
      type: [String, Number, Object],
      required: true
    },
    // 文章id
    artId: {
      type: [String, Number, Object],
      default: null
    }
  },
  data () {
    return {
      value: ''
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    // 点击了发布
    async onPostComment () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      //   console.log(this.target)
      //   console.log(this.value)
      //   console.log(this.artId)
      try {
        const { data: res } = await postComment({
          target: this.target + '',
          content: this.value,
          art_id: this.artId ? (this.artId + '') : this.artId
        })
        console.log(res.data.new_obj)
        res.data.new_obj.is_liking = false
        this.$emit('onpostSuccuss', res.data.new_obj)
        this.value = ''
        this.$toast('发布成功')
      } catch (err) {
        this.$toast('发布评论失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
